package edu.hubu.service.impl;

import edu.hubu.commons.Object2List;
import edu.hubu.commons.Object2Map;
import edu.hubu.commons.ResultsJson;
import edu.hubu.commons.StatusCode;
import edu.hubu.dao.impl.IScoreDao;
import edu.hubu.dao.impl.IStudentCourseDao;
import edu.hubu.dao.impl.IStudentDao;
import edu.hubu.dao.impl.ITeacherCourseDao;
import edu.hubu.exception.InformationNotFoundException;
import edu.hubu.model.*;
import edu.hubu.service.AbstractService;
import net.moon.jpa.dao.IBaseDao;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @author moonlan
 * date 2020/11/13 20:49
 */
@SuppressWarnings(value = "unused")
public class TeacherService extends AbstractService<Teacher> {
    private final ITeacherCourseDao teacherCourseDao;

    private final IStudentDao studentDao;

    private final IStudentCourseDao studentCourseDao;

    private final IScoreDao scoreDao;


    protected TeacherService(IBaseDao<Teacher, String> dao, ITeacherCourseDao teacherCourseDao, IStudentDao studentDao, IStudentCourseDao studentCourseDao, IScoreDao scoreDao) {
        super(dao);
        this.teacherCourseDao = teacherCourseDao;
        this.studentDao = studentDao;
        this.studentCourseDao = studentCourseDao;
        this.scoreDao = scoreDao;
    }

    @Override
    public ResultsJson findByLikePage(int currentPage, int size, Map<String, Object> map) {
        ResultsJson page = super.findByLikePage(currentPage, size, map);
        List<Teacher> users = Object2List.toList(page.getData(), Teacher.class);
        List<Object> collect = users.stream().map(Object2Map::filterMap).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, page.getTotal(), page.getCurrent(), collect);
    }

    @Override
    public ResultsJson findAllPage(int currentPage, int size) {
        ResultsJson page = super.findAllPage(currentPage, size);
        List<Teacher> users = Object2List.toList(page.getData(), Teacher.class);
        List<Object> collect = users.stream().map(Object2Map::filterMap).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, page.getTotal(), page.getCurrent(), collect);
    }

    public ResultsJson updateById(String id, Teacher teacher) {
        return super.updateById(id, teacher, t -> !t.isTeacherIsDeleted());
    }

    @Override
    public ResultsJson deleteById(String id) {
        return super.deleteById(id);
    }

    @Override
    public ResultsJson add(Teacher teacher) {
        return super.add(teacher);
    }

    public ResultsJson count() {
        return super.count("user");
    }

    public ResultsJson getCourses() {
        List<TeacherCourse> all = teacherCourseDao.findAll().stream().filter(teacher -> !teacher.isTeacherCourseIsDeleted() && !teacher.isTeacherCourseIsForbidden()).collect(Collectors.toList());
        return ResultsJson.toJson(StatusCode.SUCCESS, all.size(), 0, all);
    }

    public ResultsJson addCourses(String sid, String courseId) {
        Teacher teacher = super.findById(sid);
        Optional<TeacherCourse> byId = teacherCourseDao.findById(courseId);
        if (byId.isPresent()) {
            TeacherCourse course = byId.get();
            course.getTeachers().add(teacher);
            teacherCourseDao.save(course);
            return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, true);
        }
        throw new InformationNotFoundException("未找到courseId=" + courseId + "的课程");
    }

    public ResultsJson delCourse(String id) {
        try {
            teacherCourseDao.deleteById(id);
            return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, true);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public ResultsJson putScore(String sid, String courseId, Score score) {
        Optional<StudentCourse> studentCourseOptional = studentCourseDao.findById(courseId);
        if (studentCourseOptional.isPresent()) {
            StudentCourse studentCourse = studentCourseOptional.get();
            Score ret = new Score();
            ret.setScoreUsualScore(score.getScoreUsualScore());
            ret.setScoreExamScore(score.getScoreExamScore());
            ret.setCourse(studentCourse);
            ret.setTerm(score.getTerm());
            Optional<Student> studentOptional = studentDao.findById(sid);
            studentOptional.ifPresent(ret::setStudent);
            Score save = scoreDao.save(ret);
            return ResultsJson.toJson(StatusCode.SUCCESS, 1, 0, save);
        }
        throw new InformationNotFoundException("id为" + sid + "的学生未找到");
    }
}
